package com.sti.platform.service.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aisino.core.server.BaseService;
import com.aisino.tools.util.page.Page;
import com.aisino.tools.util.page.Pagination;
import com.sti.platform.dao.GroupDao;
import com.sti.platform.dao.UserDao;
import com.sti.platform.model.Group;
import com.sti.platform.model.User;
import com.sti.platform.model.UserGroup;
import com.sti.platform.service.GroupService;

@Service
public class GroupServiceImpl extends BaseService implements GroupService {
	@Autowired
	private GroupDao groupDao;
	@Autowired
	private UserDao userDao;

	@Override
	public List<Group> queryList(String params, Integer index, Integer row) {
		StringBuilder builder = new StringBuilder();
		String sql = "SELECT * FROM sys_group WHERE 1=1";
		builder.append(sql);
		if(params != null && !"".equals(params)){
			builder.append(" and name like '%"+params+"%'");
		}
		if(index==null || index==0){//当前页数
			index=1;
		}
		if(row==null || row==0){//每页多少条
			row=10;
		}
		Page page = new Pagination();
		page.setPage(index);
		page.setRow(row);
		return groupDao.queryList(builder.toString(),page);
	}

	@Override
	public void addGroup(Group group,String userIds) {
		super.save(group);
		//为用户组分配用户
		if(userIds != null&&!"".equals(userIds)){
			List<String> list = Arrays.asList(userIds.split(","));
			List<UserGroup> userGroups = groupDao.queryUserGroup(group.getId());
			if(userGroups.size() > 0){
				for(UserGroup userGroup: userGroups){
					super.remove(UserGroup.class,userGroup.getId());
				}
			}
			for(String li : list){
				UserGroup userGroup = new UserGroup();
				userGroup.setGroupId(group.getId());
				userGroup.setUserId(li);
				super.save(userGroup);
			}
		}
		
	}

	@Override
	public void updateStatu(String groupId, Integer status) {
		Group group = super.get(Group.class, groupId);
		group.setStatus(status);
		super.update(group);
	}

	@Override
	public void updateStatus(List<String> list, Integer status) {
		for(String li :list){
			Group group = super.get(Group.class, li);
			group.setStatus(status);
			super.update(group);
		}
	}

	@Override
	public void delectGroup(String groupId) {
		// 删除与该用户组有关联表数据
		List<UserGroup> glbs = groupDao.queryUserGroup(groupId);
		if(glbs.size()> 0){
			for(UserGroup glb :glbs){
				super.remove(UserGroup.class,glb.getId());
			}
		}
		super.remove(Group.class, groupId);
	}
	
	@Override
	public void delectGroups(String groupIds) {
		List<String> groups = Arrays.asList(groupIds.split(","));
		for(String group : groups){
			this.delectGroup(group);
		}
	}
	
	@Override
	public Group queryGroup(String groupId) {
		return super.get(Group.class, groupId);
	}

	@Override
	public List<User> queryUser(String groupId) {
		String sql = "SELECT u.* ,c.name AS \"company\" FROM sys_user u"
				+ " LEFT JOIN sys_company c ON u.company_id = c.id"
				+ " LEFT JOIN sys_user_group ug ON u.id = ug.user_id"
				+ " WHERE u.status = 0 and ug.group_id = '" + groupId + "'";
		return userDao.queryUser(sql);
	}

	@Override
	public List<User> queryUsers(String groupId) {
		String sql =  "SELECT u.* ,c.name AS \"company\" FROM sys_user u"
				+ " LEFT JOIN sys_company c ON u.company_id = c.id"
				+ " where u.status = 0 ";
		List<User> list = userDao.queryUser(sql);
		List<User> users = this.queryUser(groupId);
		for(User li :list){
			for(User user :users){
				if(li.getId().equals(user.getId())){
					li.setChecked(true);
				}
			}
		}
		return list;
	}
	

}
